package com.vrv.vap.baseline.service.impl;

import com.vrv.vap.baseline.entity.MyTicket;
import com.vrv.vap.baseline.mapper.MyTicketMapper;
import com.vrv.vap.baseline.service.AbstractExcelExportService;
import com.vrv.vap.baseline.util.FileOperationUtil;
import com.vrv.vap.baseline.config.ExcelTemplateConfig;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class MyTicketExportService extends AbstractExcelExportService<MyTicket> {
    private static final String key = "myticket";
    private final MyTicketMapper myTicketMapper;

    public MyTicketExportService(FileOperationUtil fileOperationUtil, ExcelTemplateConfig excelTemplateConfig, MyTicketMapper myTicketMapper) {
        super(fileOperationUtil, excelTemplateConfig, key);
        this.myTicketMapper = myTicketMapper;
    }

    @Override
    protected List<MyTicket> getAllData() {
        return myTicketMapper.selectAll();
    }

    @Override
    protected void processData(List<MyTicket> dataList, String format) throws IOException {
        for (MyTicket ticket : dataList) {
            // 处理formData字段
            String formData = ticket.getFormData();
            if (formData != null && formData.length() > maxCellLength) {
                String fileName = ticket.getGuid() + "_form.txt";
                File file = fileOperationUtil.createFile(fileName, key, format);
                fileOperationUtil.writeContentToFile(file, formData);
                ticket.setFormData(fileName);
            }

            // 处理flowContent字段
            String flowContent = ticket.getFlowContent();
            if (flowContent != null && flowContent.length() > maxCellLength) {
                String fileName = ticket.getGuid() + "_flow.txt";
                File file = fileOperationUtil.createFile(fileName, key, format);
                fileOperationUtil.writeContentToFile(file, flowContent);
                ticket.setFlowContent(fileName);
            }

            // 处理bpmnJson字段
            String bpmnJson = ticket.getBpmnJson();
            if (bpmnJson != null && bpmnJson.length() > maxCellLength) {
                String fileName = ticket.getGuid() + "_bpmn.txt";
                File file = fileOperationUtil.createFile(fileName, key, format);
                fileOperationUtil.writeContentToFile(file, bpmnJson);
                ticket.setBpmnJson(fileName);
            }
        }
    }

    @Override
    protected Class<MyTicket> getEntityClass() {
        return MyTicket.class;
    }
}